<template>
  <div>
    <div class="topContainer">
      <div class="general-info-t">
        <div class="general-info-avatar">
          <img
            :src="$url + userInfo.avatar_url"
            v-if="userInfo.avatar_url"
            alt=""
          />
          <img
            class="upload"
            src="../assets/img/upload.png"
            alt=""
            @click="changeDialogVisible"
          />
        </div>
        <div class="general-info-t-info">{{ userInfo.username }}</div>
      </div>
    </div>
    <div class="mainContainer">
      <div class="base-info-head">基本信息</div>
      <div class="base-info-content">
        <div class="base-info-content-show">
          <ul>
            <li>
              <div class="content-show-l">用户昵称</div>
              <div class="content-show-r">
                <template>
                  <span v-if="!infoChange">{{ userInfo.username }}</span>
                </template>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <el-dialog title="请选择头像" :visible.sync="dialogVisible" width="50%">
      <el-upload
        class="avatar-uploader"
        :action="$url+'/users/upload'"
        :show-file-list="false"
        :on-success="successUpload"
        :headers="myHeaders"
      >
        <i class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getUserInfoByToken } from '../api/user';
export default {
  data() {
    return {
      infoChange: false,
      dialogVisible: false,
      myHeaders: {
        Authorization: this.$store.state.authorization,
      },
      userInfo: {},
    };
  },
  methods: {
    changeDialogVisible() {
      this.dialogVisible = true;
    },
    successUpload(res) {
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '头像上传成功',
        });
        this.getUserInfo();
        this.$store.commit('changeAvatarUrl', res.data.path);
        this.dialogVisible = false;
      }
    },
    async getUserInfo() {
      const res = await getUserInfoByToken(this.myHeaders.Authorization);
      if (res.code !== 200) return;
      this.userInfo = res.data;
    },
  },
  created() {
    this.getUserInfo();
  },
};
</script>

<style lang="less" scoped>
.topContainer {
  margin-top: 20px;
  width: 1000px;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 10px;
  background: #fff;
  .general-info-t {
    padding: 24px 0 40px 24px;
    display: flex;
    .general-info-avatar {
      flex-shrink: 0;
      position: relative;
      width: 96px;
      height: 96px;
      margin-right: 16px;
      border: 2px solid #fff;
      border-radius: 50%;
      cursor: pointer;
      &:hover img,
      .upload {
        opacity: 0.7;
        display: block;
      }
      img {
        width: 100%;
        height: 100%;
        display: block;
        border-radius: 50%;
      }
      .upload {
        position: absolute;
        top: 0;
        width: 100%;
        opacity: 0.5;
        display: none;
      }
    }
    .general-info-t-info {
      color: #222226;
      font-weight: 500;
      line-height: 45px;
      font-size: 20px;
      margin-right: 16px;
    }
  }
}
.mainContainer {
  width: 1000px;
  background: #fff;
  margin: 0 auto;
  margin-bottom: 10px;
  .base-info-head {
    height: 48px;
    line-height: 48px;
    font-size: 18px;
    font-weight: 600;
    color: #2e2e2e;
    padding-left: 16px;
    border-bottom: 1px solid #f0f0f2;
  }
  .base-info-content {
    padding: 16px;
    .base-info-content-show {
      padding: 16px 58px 16px 0;
      position: relative;
      ul {
        border: 0;
        font-size: 100%;
        vertical-align: baseline;
        margin: 0;
        padding: 0;
        li {
          display: flex;
          .content-show-l {
            width: 56px;
            font-size: 14px;
            color: #555666;
            margin-right: 40px;
          }
          .content-show-r {
            color: #222226;
            word-break: break-all;
          }
        }
      }
    }
  }
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
  display: block;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: auto;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
</style>
